package com.wanxi.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Doteon
 * @createDate:2023/3/15
 * @version: 1.0
 */
@Configuration
public class EsConfig {
    private String esUrl;

    @Bean
    RestHighLevelClient configRestHighLevelClient() throws Exception {
        esUrl="127.0.0.1:9200";
        String[] esUrlArr = esUrl.split(",");

        List<HttpHost> httpHosts = new ArrayList<>();
        for (String es : esUrlArr) {
            String[] esUrlPort = es.split(":");
            httpHosts.add(new HttpHost(esUrlPort[0], Integer.parseInt(esUrlPort[1]), "http"));
        }
        return new RestHighLevelClient(
                RestClient.builder(httpHosts.toArray(new HttpHost[0]))
        );

    }
}
